﻿package{
	import flash.display.MovieClip;
	import flash.text.TextField;
	import flash.net.URLRequest;
	import flash.display.Loader;
	import flash.events.KeyboardEvent;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.ui.Keyboard;
	import fl.transitions.Tween;
	import fl.transitions.TransitionManager;
	import fl.transitions.TweenEvent;
	import fl.transitions.easing.*;
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.text.TextField;
	import flash.text.TextFormat;
	public class Purchase extends MovieClip
 	{
		private var boy_mc:Boy;
		//private var myMovingBoy:MovingBoy;
		private var raju_leftFrame:ImageToMovie;
		private var rajuTween:Tween;
		private var shopkeeperTween:Tween;
		private var shopkeeperTween2:Tween;
		private var shopkeeperTween3:Tween;
		private var shopkeeperTween4:Tween;
		private var shopkeeper_bubble:MovieClip;
		private var correct:MovieClip;
		private var wrong:MovieClip;
		private var dialogue_shopkeeper:Sound;
		private var dialogue_shopkeeper2:Sound;
		private var soundChannel_shopkeeper:SoundChannel;
		private var wordSound:Sound;
		private var wordSoundChannel:SoundChannel;
		private var moveSelector:Boolean;
		private var bottomBar:ImageToMovie;
		private var buttonTextLeft:TextField;
		private var buttonTextRight:TextField;
		private var textStyling:TextFormat;
		private var repeat:MovieClip;
		private var nextWord:MovieClip;
		public var image:Loader;
		public var answers:Array;
		public var URLImage:URLRequest;
		public var sequence:Array;
		public var current_selection:int;
		public var answer_selector:MovieClip;
		
		public var gameFinished:Boolean;
		public var guessingTheGame:Boolean;
		public var levelComplete:Boolean;
		
		
		public function Purchase() {
			
			//FarmGame.populateSyllabus();
			levelComplete = false;
			answers = new Array(4);
			sequence = new Array(4);
			gameFinished = false;
			shopkeeper_bubble = new Mother_bubble();
			soundChannel_shopkeeper = new SoundChannel();
			wordSoundChannel = new SoundChannel();
			guessingTheGame = false;
			//boy_mc = new Boy(10,0,800,100,355,null);
			//setupRajuAnimation();
			setupShopkeeper();
			//startRajuAnimation();
			//this.addEventListener(Event.ADDED_TO_STAGE, enteredFrame);
			moveSelector = false;
			correct = new Correct();
			wrong = new Wrong();
			setUpButtons();
		}
		
		
		/*private function setupRajuAnimation():void {			
			addChild(boy_mc);
			boy_mc.gotoAndStop("right");
			
			myMovingBoy = new MovingBoy();
			myMovingBoy.scaleY = 0.4;
			myMovingBoy.y = 370;
			myMovingBoy.scaleX = .8
			//myMovingBoy.y = 260;
			
			addChild(myMovingBoy);
			//myMovingBoy.visible = true;
			//myMovingBoy.stop();
			
			boy_mc.setY(260/0.4);
			rajuTween = new Tween(myMovingBoy,"x", None.easeIn,0,470,7,true);
			//rajuTween = new Tween(boy_mc,"x",None.easeIn,0,470,7,true);			
			rajuTween.stop();
		}*/
		/*private function startRajuAnimation():void
		{
			boy_mc.gotoAndStop("right");
			boy_mc.visible = false;
			//myMovingBoy.start();
			rajuTween.start();
			rajuTween.addEventListener(TweenEvent.MOTION_FINISH, startTheDialogues);			
		}*/
		private function setupShopkeeper():void
		{
			dialogue_shopkeeper = new Sound(new URLRequest("Audio/dialogue6.mp3"));
			dialogue_shopkeeper2 = new Sound(new URLRequest("Audio/dialogue7.mp3"));
			
			shopkeeperTween = new Tween(shopkeeper_bubble,"alpha",Regular.easeIn,0,1,0.5,true);			
			shopkeeperTween.stop();
			
			shopkeeperTween2 = new Tween(shopkeeper_bubble,"alpha",Regular.easeIn,0,1,0.5,true);
			shopkeeperTween2.stop();
			
			shopkeeperTween3 = new Tween(shopkeeper_bubble,"alpha",Regular.easeIn,1,0,0.5,true);
			shopkeeperTween3.stop();
			
			shopkeeperTween4 = new Tween(shopkeeper_bubble,"alpha",Regular.easeIn,1,0,0.5,true);
			shopkeeperTween4.stop();
			
			addChild(shopkeeper_bubble);
			shopkeeper_bubble.visible = false;
			
			answer_selector = new AnswerSelection();
			addChild(answer_selector);
			answer_selector.visible = false;
		}
		
		
		//private function startTheDialogues( e:Event):void {
		public function startTheDialogues(bubbleX:Number, bubbleY:Number):void {
			//rajuTween.removeEventListener(TweenEvent.MOTION_FINISH, startTheDialogues);
			//boy_mc.x = 470;
			//boy_mc.visible = true;
			//myMovingBoy.visible = false;
			//removeChild(myMovingBoy);
			this.addEventListener(Event.ENTER_FRAME, enteredFrame);
			guessingTheGame = true;
			shopkeeper_bubble.visible = true;
			shopkeeper_bubble.x = bubbleX;
			shopkeeper_bubble.y = bubbleY;
			shopkeeper_bubble.mother_speech.text = "What do you want, Raju?";
			shopkeeperTween.start();
			shopkeeperTween.addEventListener(TweenEvent.MOTION_FINISH,shopkeeperDialogue1Finished);
			
			
			
		}
		private function shopkeeperDialogue1Finished(e:Event):void
		{
			shopkeeperTween.removeEventListener(TweenEvent.MOTION_FINISH,shopkeeperDialogue1Finished);
			soundChannel_shopkeeper = dialogue_shopkeeper.play();
			soundChannel_shopkeeper.addEventListener(Event.SOUND_COMPLETE, startTheGuessGame);
			
		}
		
		private function startTheGuessGame(e:Event):void {
			soundChannel_shopkeeper.removeEventListener(Event.SOUND_COMPLETE, startTheGuessGame);
			shopkeeperTween3.start();
			shopkeeperTween3.addEventListener(TweenEvent.MOTION_FINISH, shopkeeperDialogue1Finished_fadeOut);
			
			
		}
		private function shopkeeperDialogue1Finished_fadeOut(e:Event):void
		{
			shopkeeperTween3.removeEventListener(TweenEvent.MOTION_FINISH, shopkeeperDialogue1Finished_fadeOut);
			//boy_mc.gotoAndStop("front");
						
			loadImagesAndSound(FarmGame.questionNumber);
			answer_selector.visible = true;			
			current_selection = 0;
			moveAnswerSelector();
			arrangeImagesRandomly();
			playSound(null);
		}
		
		public function loadImagesAndSound(myIndex:int):void {
			/*if(Market.questionNumber>1)
			{
				removeChild(correct);
				removeChild(wrong);
			}*/
			wordSound = new Sound();
			wordSound.load(new URLRequest("Audio/" + FarmGame.syllabus[myIndex-1].audio));
			loadImage("Images/" + FarmGame.syllabus[myIndex-1].image, 1);
			loadImage("Images/" + FarmGame.syllabus[myIndex-1].image, 2);
			loadImage("Images/" + FarmGame.syllabus[myIndex-1].image, 3);
			loadImage("Images/" + FarmGame.syllabus[myIndex-1].image, 4);
			/*loadImage("Images/" + FarmGame.syllabus[myIndex-1].phonological_dist+".png", 2);
			loadImage("Images/" + FarmGame.syllabus[myIndex-1].semantic_dist+".png", 3);
			loadImage("Images/" + FarmGame.syllabus[myIndex-1].unrelated_dist+".png", 4);*/
			addChild(correct);
			addChild(wrong);
			correct.visible = false;
			wrong.visible = false;
		}
		
		private function hidePreviousImages():void
		{
			var my_i:int;
			for (my_i = 1;my_i<=4;my_i = my_i + 1)
			{
				removeChild(answers[my_i].movie)
			}
			removeChild(correct);
			removeChild(wrong);
			answer_selector.visible = false;
			bar_invisible();
		}
		public function loadImage(filename:String, i:Number):void {
			URLImage = new URLRequest(filename);
			image = new Loader();
			image.load(URLImage);
			answers[i] = new Object();
			answers[i].imageName = filename.substring(7);
			
			answers[i].movie = new MovieClip();
			addChild(answers[i].movie);
			answers[i].movie.addChild(image);
			
		}
		
		public function playSound(event:Event):void {
			
			if (wrong.visible == true)
			{
				shopkeeperTween4.start();
				shopkeeperTween4.addEventListener(TweenEvent.MOTION_FINISH, shopkeeperDialogue2Finished_fadeOut);
			}
			else
			{
				wordSoundChannel = wordSound.play();
				wordSoundChannel.addEventListener(Event.SOUND_COMPLETE, soundIsComplete);
			}
		}
		
		private function shopkeeperDialogue2Finished_fadeOut(e:Event):void
		{
			shopkeeperTween4.removeEventListener(TweenEvent.MOTION_FINISH, shopkeeperDialogue2Finished_fadeOut);
			correct.visible = false;
			wrong.visible = false;
			arrangeImagesRandomly();
			current_selection = 0;
			moveAnswerSelector();
			wordSoundChannel = wordSound.play();
			wordSoundChannel.addEventListener(Event.SOUND_COMPLETE, soundIsComplete);
			
		}
		
		public function soundIsComplete(e:Event):void {
			bar_visible_repeatWord();
			moveSelector = true;
		}
		
		public function arrangeImagesRandomly():void {
			var initial_array:Array = new Array(1,2,3,4);
			var i:int;
			var next_number:Number;
			
			for(i=0;i<4;i++)
			{
				next_number = Math.floor(Math.random()*initial_array.length);
				sequence[i]=initial_array[next_number];
				initial_array[next_number]=initial_array[initial_array.length-1];
				initial_array.pop();
				
				if(i%2==0)
					answers[sequence[i]].movie.x = 50;
				else
					answers[sequence[i]].movie.x = 250;
					
				if(i<2)
					answers[sequence[i]].movie.y = 50;
				else
					answers[sequence[i]].movie.y = 250;
			}
		}
		
		public function enteredFrame(e:Event):void {
			stage.addEventListener(KeyboardEvent.KEY_DOWN,moveSelection);
			
			removeEventListener(Event.ENTER_FRAME, enteredFrame);
		}
		
		public function moveSelection(e:KeyboardEvent):void 
		{
			
			if(repeat.visible == true)
			{
				if (e.keyCode == Keyboard.SPACE)
				{
					moveSelector = false;
					playSound(null);
				}
			}
			
			if (nextWord.visible == true)
			{
				if (e.keyCode == Keyboard.ENTER)
				{
					guessingTheGame = false;
					levelComplete = true;
					
					loadTheNextQuestion();
					
				}
			}
			if(moveSelector == true)
			{
				if (e.keyCode == Keyboard.RIGHT && current_selection%2==0) {
					current_selection++;
				} else if (e.keyCode == Keyboard.LEFT && current_selection%2==1) {
					current_selection--;
				} else if (e.keyCode == Keyboard.UP && current_selection>1) {
					current_selection=current_selection-2;
				} else if (e.keyCode == Keyboard.DOWN && current_selection<2) {
					current_selection=current_selection+2;
				} else if (e.keyCode == Keyboard.ENTER) {
					if (checkAnswer()==true)
						congratulations();
					else
						tryAgain();
				}
				moveAnswerSelector();
			}
		}
		
		private function checkAnswer():Boolean {
			moveSelector = false;
			bar_invisible();
			if(answers[sequence[current_selection]].imageName == FarmGame.syllabus[FarmGame.questionNumber-1].image)
				return true;
			else
				return false;
		}
		
		private function congratulations(): void {
			
			moveSelector = false;
			correct.visible = true;
			correct.x = answers[sequence[current_selection]].movie.x + 37.5;
			correct.y = answers[sequence[current_selection]].movie.y + 37.5;
			bar_visible_nextWord();
			SyllabusQueue.updateWordWeight(FarmGame.syllabus[FarmGame.questionNumber-1].name,1);
			//nextWord.addEventListener(MouseEvent.CLICK, loadTheNextQuestion);
		}
		
		public function updateSyllabusList_1():void
		{
			if (FarmGame.questionNumber<=5)
				SyllabusQueue.updateWordWeight(FarmGame.syllabus[FarmGame.questionNumber-1].name,1);
			
			if (FarmGame.questionNumber>5)
			{
				FarmGame.questionNumber = 1;
				SyllabusQueue.updateSyllabusQueue();
			}			
			
		}
		private function loadTheNextQuestion():void
		{
			//nextWord.removeEventListener(MouseEvent.CLICK, loadTheNextQuestion);
			
			stage.removeEventListener(KeyboardEvent.KEY_DOWN,moveSelection);
			FarmGame.questionNumber++;
			//levelComplete = false;
			
			
			
			if(FarmGame.questionNumber>5) {
				
				FarmGame.questionNumber = 1;
				//removeChild(myMovingBoy);
				gotoNextRound();
				return;
			}
			hidePreviousImages();
			//startRajuAnimation();			
			//startTheDialogues(null);
		}
		
		private function tryAgain(): void {
			wrong.visible = true;
			wrong.x = answers[sequence[current_selection]].movie.x + 37.5;
			wrong.y = answers[sequence[current_selection]].movie.y + 37.5;
			
			shopkeeperDialogue("Audio/dialogue7.mp3","Try Again!");
		}
		
		private function shopkeeperDialogue(audio:String, text:String):void {
			
			shopkeeper_bubble.visible = true;
			shopkeeperTween2.start();
			shopkeeperTween2.addEventListener(TweenEvent.MOTION_FINISH,shopkeeperDialogue2Finished);
			shopkeeper_bubble.x = 570;
			shopkeeper_bubble.y = 150;
			shopkeeper_bubble.mother_speech.text = text;
		}
		private function shopkeeperDialogue2Finished(e:Event):void
		{
			shopkeeperTween2.removeEventListener(TweenEvent.MOTION_FINISH,shopkeeperDialogue2Finished);
			soundChannel_shopkeeper = dialogue_shopkeeper2.play();
			soundChannel_shopkeeper.addEventListener(Event.SOUND_COMPLETE, playSound);
		}
		
		private function gotoNextRound():void {
			SyllabusQueue.updateSyllabusQueue();
			
			
			
			//Now transfer control back to the narrative screen
			gameFinished = true;
			hidePreviousImages();
			//removeChild(boy_mc);						
		}
		
		public function moveAnswerSelector():void {
			if(current_selection%2==0)
				answer_selector.x = 50;
			else
				answer_selector.x = 250;
					
			if(current_selection<2)
				answer_selector.y = 50;
			else
				answer_selector.y = 250;
		}
		
		public function setUpButtons()
		{
			repeat = new MovieClip();
			nextWord = new MovieClip();
			bottomBar = new ImageToMovie("Images/horizontalBar.jpg");
			bottomBar.setX(0);
			bottomBar.setY(453);
			bottomBar.scaleX = 1.61;
			
			buttonTextLeft = new TextField();
			buttonTextRight = new TextField();
			softKeys("Repeat", "Next");
			textStyling = new TextFormat();
			textStyling.size = 24;
			textStyling.color = 0xFFFF00;
			buttonTextLeft.setTextFormat(textStyling);
			buttonTextRight.setTextFormat(textStyling);
			bar_invisible();
			addChild(bottomBar);
			addChild(repeat);
			addChild(nextWord);
			
			repeat.addChild(buttonTextLeft);
			repeat.x = 20;
			repeat.y = 450;
			nextWord.addChild(buttonTextRight);
			nextWord.x = 710;
			nextWord.y = 450;
		}
		
		public function softKeys(textLeftKey:String, textRightKey:String) {
			buttonTextLeft.text = textLeftKey;
			buttonTextRight.text = textRightKey;
		}
		
		
		
		public function bar_invisible():void {
			bottomBar.hideImage();
			repeat.visible = false;
			nextWord.visible = false;
		}
		
		public function bar_visible_nextWord():void {
			bottomBar.showImage();
			repeat.visible = false;
			nextWord.visible = true;
		}
		public function bar_visible_repeatWord():void
		{
			bottomBar.showImage();
			repeat.visible = true;
			nextWord.visible = false;
		}
	}
}